package com.example.jcservice.controller;

import com.example.jcservice.service.LibraryService;
import com.example.jcservice.stType.Resive.Library.LibraryUserInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;


@RestController
public class LibraryController {
    //@Autowired 依赖注入写法
    @Resource
    private LibraryService service;

    /**
     * 测试接口，获取个人账户基本信息
     * */
    @GetMapping("/test")
    public Map<String, Object> sayHello() {
        Map<String, Object> result = new HashMap<>();
        result.put("message", "显示这句话，说明LibraryLoginController正常运行");
        return result;
    }

    /**
     *图书馆登录接口
     * */
    @GetMapping("/librarylogin")
    public LibraryUserInfo demo(@RequestParam("username") String username, @RequestParam("password") String password) throws UnsupportedEncodingException {
        System.out.println("传入参数 学号：" + username);
        System.out.println("传入参数 密码：" + password);

        //Java Base64转码，此方法适用于Java8后，详情网址 https://www.cnblogs.com/alter888/p/9140732.html
        //final Base64.Decoder decoder = Base64.getDecoder();
        final Base64.Encoder encoder = Base64.getEncoder();
        final String Base64 = password;
        final byte[] textByte = Base64.getBytes("UTF-8");
        //编码
        final String passBase64 = encoder.encodeToString(textByte);
        System.out.println("Base64转码后密码为： "+passBase64);
        //解码
        //System.out.println(new String(decoder.decode(encodedText), "UTF-8"));

        LibraryUserInfo result = service.login(username, passBase64);

        return result;
    }
}

